package dk.hewison.client.mvc;

import dk.hewison.client.AppEvent;
import dk.hewison.client.Log;


/**
 * @author John Hewison (creator)
 * @author $LastChangedBy: john.hewison $ $LastChangedDate: 2009-02-15 12:47:07 +0000 (Sun, 15 Feb 2009) $
 * @version $Revision: 364 $
 */
public abstract class AbstractController<T> implements Controller<T> {

    private int id;
    private String name;

    protected View<T> view;
    private Action[] actionList;
    private Controller[] subControllers;
    private boolean enabled = true;

    protected AbstractController() {
        this(0);
    }

    public AbstractController(int id) {
        this(id, "");
    }

    protected AbstractController(int id, String name) {
        this.id = id;
        this.name = name;
        actionList = initActions();
        subControllers = initSubControllers();
        //updateActions();
    }


    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public boolean isEnabled() {
        return enabled;
    }

    protected Action[] initActions() {
        return new Action[0];
    }


    protected Controller[] initSubControllers() {
        return new Controller[0];
    }


    public View<T> getView() {
        if (view == null) {
            view = createView();
            view.setController(this);
            view.initView();

            showPage();
            updateActions();
        }
        return view;
    }


    public View getSubView(int id) {
        Controller subController = getSubController(id);
        if (subController != null) {
            return subController.getView();
        }
        throw new RuntimeException("SubController not found " + id);
    }

    public Controller getSubController(int id) {
        for (Controller subController : subControllers) {
            if (id == subController.getId()) {
                return subController;
            }
        }
        return null;
    }

    public void updateActions() {
        if (actionList == null) return;
        for (Action action : actionList) {
            action.updateState();
        }
    }

    public Action getAction(int actionId) {
        if (actionList == null) return null;
        for (Action action : actionList) {
            if (actionId == action.getId()) {
                return action;
            }
        }
        return null;
    }

    public Action[] getActions() {
        return actionList;
    }


    public Log log() {
        return Log.getInstance();
    }

    public void showPage() {
    }

    public boolean leavePage() {
        return true;
    }

    public void eventOccurred(AppEvent event) {

    }

    public void fireEvent(AppEvent event) {
        Register.getInstance().eventOccurred(event);
    }

    public void registerEvent(AppEvent.Event eventId) {
        Register.getInstance().registerEvent(eventId, this);
    }
}
